Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 [Bug-Fix]: add lock to avoid data race #2360 #2368

Merged
merged 1 commit into from
Mar 14, 2023

Conversation

yanke-xu
Copy link
Contributor

@yanke-xu yanke-xu commented Mar 13, 2023

The fix is to protect the access to s.db and save the result to a local variable.

Description
Fixed inconsistency and also potential data race in proto/table_unmarshal.go:
u.reqFields is read/written 4 times in proto/table_unmarshal.go; 3 out of 4 times it is protected by u.lock.Lock(); 1 out of 4 times it is read without a Lock, which is in func unmarshal() on L260.
A data race may happen when unmarshal() and other func like computeUnmarshalInfo() are called in parallel.

In order to avoid potential data race here, I use u.lock.RLock(); defer u.lock.RUnlock() to make sure that all usages of items is in critical section.

Fixes #2360

Type of change
[ ] Bug fix (non-breaking change which fixes an issue)
[ ] Breaking change (fix or feature that would cause existing functionality to not work as expected)

Checklist:
For new functionalities I follow the inspiration of the express js framework and built them similar in usage
[ ] I have performed a self-review of my own code
[ ] I have commented my code, particularly in hard-to-understand areas
[ ] I have made corresponding changes to the documentation - /docs/ directory for https://docs.gofiber.io/
[ ] I have added tests that prove my fix is effective or that my feature works
[ ] New and existing unit tests pass locally with my changes
[ ] If new dependencies exist, I have checked that they are really necessary and agreed with the maintainers/community (we want to have as few dependencies as possible)
[ ] I tried to make my code as fast as possible with as few allocations as possible
[ ] For new code I have written benchmarks so that they can be analyzed and improved

The fix is to protect the access to s.db and save the result to a local variable.
@welcome
Copy link

welcome bot commented Mar 13, 2023

Thanks for opening this pull request! 🎉 Please check out our contributing guidelines. If you need help or want to chat with us, join us on Discord https://gofiber.io/discord

@yanke-xu yanke-xu changed the title 🐛 [Bug-Fix]:add lock 🐛 [Bug-Fix]:add lock to avoid data race #2360 Mar 13, 2023
@yanke-xu yanke-xu changed the title 🐛 [Bug-Fix]:add lock to avoid data race #2360 🐛 [Bug-Fix]: add lock to avoid data race #2360 Mar 13, 2023
@gaby
Copy link
Member

gaby commented Mar 13, 2023

Now that I look at it, I think Conn() was suppose to return a pointer. Like the other Storage providers do.

@ReneWerner87 ReneWerner87 merged commit 678728d into gofiber:master Mar 14, 2023
@welcome
Copy link

welcome bot commented Mar 14, 2023

Congrats on merging your first pull request! 🎉 We here at Fiber are proud of you! If you need help or want to chat with us, join us on Discord https://gofiber.io/discord

@leonklingele
Copy link
Member

leonklingele commented Mar 14, 2023 via email

@ReneWerner87
Copy link
Member

ReneWerner87 commented Mar 14, 2023

okay, then we optimize it further

@UtopiaGitHub can you provide example code where we can then get the race condition so we can compare the old and new condition/behavior

@leonklingele
Copy link
Member

Can we revert this change?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

🐛 [Bug]: memory: protect field access with lock to avoid possible data race
4 participants